import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_video_course.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';
import 'package:flutter_learn_vi/src/page/study/video_search_item_page.dart';
import 'package:flutter_learn_vi/src/utils/string_utils.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/2/4.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 视频搜索
class VideoSearchPage extends StatefulWidget {
  @override
  _VideoSearchPageState createState() => _VideoSearchPageState();
}

class _VideoSearchPageState extends State<VideoSearchPage> {
  String _tipsString = "请输入关键字进行搜索";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,

      ///填充布局
      body: Container(
          width: double.infinity,
          height: double.infinity,
          padding: EdgeInsets.only(top: 24),
          child: Column(
            children: [
              buildRow(context),
              Expanded(
                child: buildListView(),
              )
            ],
          )),
    );
  }

  Row buildRow(BuildContext context) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        IconButton(
          icon: Icon(Icons.arrow_back_ios_outlined),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
        Expanded(
          child: Container(
            height: 44,
            child: Container(
              alignment: Alignment.center,
              //内边距
              padding: EdgeInsets.only(left: 10, right: 10),
              //圆角背景
              decoration: BoxDecoration(
                  color: Colors.grey[200],
                  borderRadius: BorderRadius.all(Radius.circular(4))),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.only(top: 12),
                      alignment: Alignment.center,
                      child: TextField(
                        maxLines: 1,
                        style: TextStyle(),
                        controller: _textEditingController,
                        focusNode: _focusNode,
                        decoration: InputDecoration(
                            hintText: "请输入搜索的视频课程名称",
                            border: OutlineInputBorder(
                                borderSide:
                                    BorderSide(style: BorderStyle.none)),
                            enabledBorder: OutlineInputBorder(
                                borderSide:
                                    BorderSide(style: BorderStyle.none)),
                            focusedBorder: OutlineInputBorder(
                                borderSide:
                                    BorderSide(style: BorderStyle.none))),
                      ),
                    ),
                  ),
                  TextButton(
                    onPressed: () {
                      setState(() {
                        _tipsString = "开始搜索";
                      });
                      getViceoCourseList();
                    },
                    child: Text("搜索"),
                  ),
                ],
              ),
            ),
          ),
        )
      ],
    );
  }

  buildListView() {
    if (_list == null || _list.length == 0) {
      return NoDataWidget(
        text: _tipsString,
      );
    }
    return VideoSearchItemPage(
      list: _list,
      itemHeight: 180,
    );
  }

  TextEditingController _textEditingController = new TextEditingController();
  FocusNode _focusNode = new FocusNode();
  List<BeanVideoCourse> _list;
  CancelToken _cancelToken;

  Future<bool> getViceoCourseList() async {
    String key = _textEditingController.text;
    if (StringUtils.isEmpty(key)) {
      _tipsString = "请输入要搜索的内容";
      ToastUtils.showToast("请输入要搜索的内容 ");
      return false;
    }
    _focusNode.unfocus();
    Map<String, dynamic> map = Map();
    map["keyWords"] = key;
    //post请求发送
    ResponseInfo responseInfo = await DioUtils.instance.getRequest(
      url: HttpHelper.videoCourseSearchUrl,
      queryParameters: map,
      cancelTag: _cancelToken,
    );

    if (responseInfo.success) {
      _list = [];
      List list = responseInfo.data;
      list.forEach((element) {
        BeanVideoCourse beanVideoCourse = BeanVideoCourse.fromMap(element);
        _list.add(beanVideoCourse);
      });
    }
    _tipsString = "搜索完成";
    if (!mounted) return true;
    setState(() {});

    return Future.value(true);
  }
}
